package com.fjw.mapper;


import com.fjw.pojo.Warehouse;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WarehouseMapper {

    // 查询所有仓库记录
    @Select("SELECT * FROM warehouse")
    List<Warehouse> findAll();

    // 根据 id 查询仓库记录
    @Select("SELECT * FROM warehouse WHERE id = #{id}")
    Warehouse findById(Integer id);

    // 新增仓库记录
    @Insert("INSERT INTO warehouse(wname, waddress, num, beizhu) VALUES (#{wname}, #{waddress}, #{num}, #{beizhu})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 自动生成主键并回显到实体类的 id 字段
    void insert(Warehouse warehouse);

    // 更新仓库记录
    @Update("UPDATE warehouse SET wname = #{wname}, waddress = #{waddress}, num = #{num}, beizhu = #{beizhu} WHERE id = #{id}")
    void update(Warehouse warehouse);

    // 根据 id 删除仓库记录
    @Delete("DELETE FROM warehouse WHERE id = #{id}")
    void deleteById(Integer id);

    // 进货操作
    @Update("UPDATE warehouse SET num = CAST(num AS SIGNED) + #{num} WHERE id = #{id}")
    void addStock(@Param("id") Integer id, @Param("num") int num);

    // 出货操作
    @Update("UPDATE warehouse SET num = CAST(num AS SIGNED) - #{num} WHERE id = #{id}")
    void reduceStock(@Param("id") Integer id, @Param("num") int num);
}